#include "egraphicsobjectmanager.h"
#include "../resources/etexture.h"
#include "../resources/etexturemanager.h"
#include "esprite.h"
#include "egraphicsobject.h"

EGraphicsObjectManager::EGraphicsObjectManager(boost::shared_ptr<ETextureManager> textureManager)
    : m_textureManager(textureManager)
{
}

boost::shared_ptr<ETextureManager> EGraphicsObjectManager::textureManager() const
{
    return m_textureManager.lock();
}

boost::shared_ptr<EGraphicsObject> EGraphicsObjectManager::loadFromTexture(const std::string &name, const std::string &texPath)
{
    boost::shared_ptr<EGraphicsObject> newObject;
    boost::shared_ptr<ETexture> texture;

    if(m_objects.find(name) != m_objects.end())
    {
        // do nothing
    }
    else if(texture = textureManager()->texture(texPath))
    {
        newObject.reset(new EGraphicsObject);
        newObject->m_sprite.reset(new ESprite);
        newObject->m_texture = texture;

        m_objects[name] = newObject;
    }

    return newObject;
}

boost::shared_ptr<EGraphicsObject> EGraphicsObjectManager::create(const std::string &name)
{
    boost::shared_ptr<EGraphicsObject> newObject;

    if(m_objects.find(name) != m_objects.end())
    {
        newObject = m_objects[name]->clone();
    }

    return newObject;
}
